<template>
  <page-card title="岗位管理"
             :sub-title="!this.$route.query.id ? '新增':'编辑'"
             :need-back="true">
    <a-form :form="form"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 12 }">
      <a-form-item label="ID"
                   v-show="false">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item label="岗位名称"
                   required>
        <a-input placeholder="岗位名称，最多40个字符"
                 :max-length="40"
                 onkeyup="this.value=this.value.replace(/[, ]/g,'')"
                 v-decorator="['jobName',{rules: [{
                   required: true, message: '请输入岗位名称'
                 }]}]" />
      </a-form-item>
      <a-form-item label="岗位编码"
                   required>
        <a-input placeholder="岗位编码，最多20个字符"
                 :max-length="20"
                 onkeyup="this.value=this.value.replace(/[, ]/g,'')"
                 v-decorator="['jobCode',{rules: [{
                   required: true, message: '请输入岗位编码'
                 }]}]" />
      </a-form-item>
      <a-form-item label="排序号"
                   required>
        <a-input-number placeholder="请输入"
                        :min="1"
                        :max="9999"
                        v-decorator="['orderNo',{rules: [{
                          required: true, message: '请输入'
                        }]}]" />
      </a-form-item>
      <a-form-item label=" "
                   :colon="false">
        <div style="text-align:center">
          <a-button type="primary"
                    action
                    @click="handleSave">保存</a-button>
        </div>
      </a-form-item>
    </a-form>
  </page-card>
</template>

<script>
import { saveOrUpdateJobInfo, getOrderNo, getJobInfoById } from '../../api/job/index'
export default {
  name: 'JobManagementEdit',
  data () {
    return {
      id: this.$route.query.id || '',
      form: this.$form.createForm(this)
    }
  },
  created () {
    if (this.id != '') {
      this.getInfoByID(this.id)
    } else {
      this.getOrderNo()
    }
  },
  methods: {
    // 保存
    handleSave (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (err) {
          return
        }
        if (this.id != '') {
          values.id = this.id
        }
        saveOrUpdateJobInfo(values).then((res) => {
          this.$router.go(-1)
        })
      })
    },
    // 排序号
    getOrderNo () {
      getOrderNo().then((res) => {
        this.form.setFieldsValue({
            orderNo: res
          })
      })
    },
    getInfoByID (id) {
      getJobInfoById(id).then(res => {
        this.form.setFieldsValue(res)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-input-number {
  width: 100%;
}
</style>
